//index.js
const app = getApp();

Page({
  data: {
    banners: [
      {
        id: 1,
        imageUrl: '../../images/banner1.png',
        link: ''
      },
      {
        id: 2,
        imageUrl: '../../images/banner2.png',
        link: ''
      },
      {
        id: 3,
        imageUrl: '../../images/banner3.png',
        link: ''
      }
    ],
    recommendedRecipes: [],
    hotRecipes: [],
    nutritionTips: [],
    searchKeyword: ''
  },

  onLoad: function() {
    // 页面加载时获取数据
    this.loadRecommendedRecipes();
    this.loadHotRecipes();
    this.loadNutritionTips();
  },

  onShow: function() {
    // 页面显示时检查登录状态
    if (app.globalData.isLoggedIn) {
      // 如果已登录，可以刷新个性化数据
    }
  },

  // 加载推荐食谱数据
  loadRecommendedRecipes: function() {
    // 这里应该调用API获取数据，现在使用模拟数据
    const mockData = [
      {
        id: 1,
        title: '低嘌呤早餐套餐',
        description: '燕麦粥、水煮蛋、凉拌黄瓜，营养均衡又健康',
        imageUrl: '../../images/recipe1.png',
        cookingTime: 15,
        difficultyLevel: '简单'
      },
      {
        id: 2,
        title: '清蒸鱼健康午餐',
        description: '清蒸鲈鱼、清炒时蔬、糙米饭，低油低盐更健康',
        imageUrl: '../../images/recipe2.png',
        cookingTime: 30,
        difficultyLevel: '中等'
      },
      {
        id: 3,
        title: '番茄鸡蛋晚餐',
        description: '番茄鸡蛋汤、凉拌黄瓜、全麦面包，清淡易消化',
        imageUrl: '../../images/recipe3.png',
        cookingTime: 20,
        difficultyLevel: '简单'
      }
    ];
    
    this.setData({
      recommendedRecipes: mockData
    });
  },

  // 加载热门食谱数据
  loadHotRecipes: function() {
    // 这里应该调用API获取数据，现在使用模拟数据
    const mockData = [
      {
        id: 4,
        title: '时蔬沙拉',
        imageUrl: '../../images/recipe4.png',
        likes: 1234
      },
      {
        id: 5,
        title: '杂粮粥',
        imageUrl: '../../images/recipe5.png',
        likes: 987
      },
      {
        id: 6,
        title: '烤鸡胸肉',
        imageUrl: '../../images/recipe6.png',
        likes: 876
      },
      {
        id: 7,
        title: '蔬菜汤',
        imageUrl: '../../images/recipe7.png',
        likes: 765
      }
    ];
    
    this.setData({
      hotRecipes: mockData
    });
  },

  // 加载营养小知识数据
  loadNutritionTips: function() {
    // 这里应该调用API获取数据，现在使用模拟数据
    const mockData = [
      {
        id: 1,
        title: '如何正确摄入蛋白质？'
      },
      {
        id: 2,
        title: '每日饮水量多少合适？'
      },
      {
        id: 3,
        title: '膳食纤维的重要性'
      }
    ];
    
    this.setData({
      nutritionTips: mockData
    });
  },

  // 搜索输入
  onSearchInput: function(e) {
    this.setData({
      searchKeyword: e.detail.value
    });
  },

  // 搜索确认
  onSearchConfirm: function() {
    if (this.data.searchKeyword.trim()) {
      // 跳转到搜索结果页面
      wx.navigateTo({
        url: '/pages/recipe/recipe?keyword=' + encodeURIComponent(this.data.searchKeyword)
      });
    }
  },

  // 导航到食材管理
  navigateToFoodManagement: function() {
    wx.navigateTo({
      url: '/pages/food-management/food-management'
    });
  },

  // 导航到食谱推荐
  navigateToRecipe: function() {
    wx.navigateTo({
      url: '/pages/recipe/recipe'
    });
  },

  // 导航到健康数据
  navigateToHealthData: function() {
    if (!app.globalData.isLoggedIn) {
      wx.navigateTo({
        url: '/pages/login/login'
      });
      return;
    }
    wx.navigateTo({
      url: '/pages/personal-center/personal-center?tab=health'
    });
  },

  // 导航到健康知识
  navigateToKnowledge: function() {
    wx.navigateTo({
      url: '/pages/knowledge/knowledge'
    });
  },

  // 导航到更多推荐食谱
  navigateToMoreRecipes: function() {
    wx.navigateTo({
      url: '/pages/recipe/recipe?type=recommended'
    });
  },

  // 导航到热门食谱
  navigateToHotRecipes: function() {
    wx.navigateTo({
      url: '/pages/recipe/recipe?type=hot'
    });
  },

  // 导航到食谱详情
  navigateToRecipeDetail: function(e) {
    const recipeId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/recipe/recipe?id=' + recipeId
    });
  },

  // 导航到知识详情
  navigateToKnowledgeDetail: function(e) {
    const knowledgeId = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: '/pages/knowledge/knowledge?id=' + knowledgeId
    });
  }
})